package com.lizixin.controller;

import com.lizixin.pojo.UserInfo;
import com.lizixin.service.UserService;
import com.lizixin.service.impl.UserServiceImpl;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;
import java.util.List;

/**
 * Created by 黎子新 on 2017/6/14.
 */
@Controller
@RequestMapping("/user")
public class TestController {

    @Autowired
    private UserService userService;

    //分页
    @Autowired
    private UserServiceImpl userServiceimpl;
//


    @RequestMapping(value = "users",method = RequestMethod.GET)
    public String QueryAllUser(Model model,HttpServletRequest request) {

        this.userServiceimpl.showUserInfoByPage(request, model);
//        List<UserInfo> list = userService.getAllUser();
//        model.addAttribute("users", list);

        return "user/users";
    }



    @RequestMapping(value ="/{id}/delete",method =RequestMethod.GET)
    public String delUser(@PathVariable Integer id){
        userService.deleteUser(id);
        return "redirect:/user/users";

    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)

    public String addUser(Model model){
        model.addAttribute(new UserInfo());
        return "user/add";


    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addUser(@Validated UserInfo userInfo) {

        userService.addUser(userInfo);

        return "redirect:/user/users";
    }


    @RequestMapping(value = "/{id}/update",method = RequestMethod.GET)
    public String updateUser(@PathVariable Integer id,Model model){
        model.addAttribute(new UserInfo());
        return "user/update";
    }

    @RequestMapping(value = "/{id}/update",method = RequestMethod.POST)
    public String update(@PathVariable Integer id,@Validated UserInfo userInfo){
        userService.update(userInfo);
        return "redirect:/user/users";
    }




    @RequestMapping(value = "/myProductPage",method = RequestMethod.GET)
    public String showitemsList(Model model,HttpServletRequest request){
        this.userServiceimpl.showUserInfoByPage(request, model);
        return "user/pageTest";
    }
}

